home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / sh-utils.12 / sh-utils / sh-utils-1.12 / src / logname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  2.3 KB  |  106 lines

  1. /* logname -- print user's login name
  2.    Copyright (C) 90, 91, 92, 93, 1994 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <config.h>
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <getopt.h>
  22.  
  23. #include "version.h"
  24. #include "system.h"
  25.  
  26. /* The name this program was run with. */
  27. char *program_name;
  28.  
  29. /* If non-zero, display usage information and exit.  */
  30. static int show_help;
  31.  
  32. /* If non-zero, print the version on standard output and exit.  */
  33. static int show_version;
  34.  
  35. static struct option const long_options[] =
  36. {
  37.   {"help", no_argument, &show_help, 1},
  38.   {"version", no_argument, &show_version, 1},
  39.   {0, 0, 0, 0}
  40. };
  41.  
  42. static void
  43. usage (status)
  44.      int status;
  45. {
  46.   if (status != 0)
  47.     fprintf (stderr, "Try `%s --help' for more information.\n",
  48.          program_name);
  49.   else
  50.     {
  51.       printf ("Usage: %s [OPTION]...\n", program_name);
  52.       printf ("\
  53. \n\
  54.   --help      display this help and exit\n\
  55.   --version   output version information and exit\n\
  56. ");
  57.     }
  58.   exit (status);
  59. }
  60.  
  61. void
  62. main (argc, argv)
  63.      int argc;
  64.      char **argv;
  65. {
  66.   register char *cp;
  67.   int c;
  68.  
  69.   program_name = argv[0];
  70.  
  71.   while ((c = getopt_long (argc, argv, "", long_options, (int *) 0)) != EOF)
  72.     {
  73.       switch (c)
  74.     {
  75.     case 0:
  76.       break;
  77.  
  78.     default:
  79.       usage (1);
  80.     }
  81.     }
  82.  
  83.   if (show_version)
  84.     {
  85.       printf ("logname - %s\n", version_string);
  86.       exit (0);
  87.     }
  88.  
  89.   if (show_help)
  90.     usage (0);
  91.  
  92.   if (argc - optind != 0)
  93.     usage (1);
  94.  
  95.   /* POSIX.2 requires using getlogin (or equivalent code).  */
  96.   cp = getlogin ();
  97.   if (cp)
  98.     {
  99.       puts (cp);
  100.       exit (0);
  101.     }
  102.   /* POSIX.2 prohibits using a fallback technique.  */
  103.   fprintf (stderr,"%s: no login name\n", argv[0]);
  104.   exit (1);
  105. }
  106.